package com.tg.service.impl;

import com.tg.mapper.EbProductCategoryMapper;
import com.tg.model.EbProductCategory;
import com.tg.service.ProductCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ProductCategoryServiceImpl implements ProductCategoryService {

    @Autowired
    private EbProductCategoryMapper ebProductCategoryMapper;

    @Override
    public Map<String, List<EbProductCategory>> findBigAndSmallCategory() {
        //1.查询所有大类
        List<EbProductCategory> bigs = ebProductCategoryMapper.selectBigCategory();
        System.out.println("所有大类："+bigs);
        //2.查询所有小类
        List<EbProductCategory> smalls = ebProductCategoryMapper.selectSmallCategory();
        System.out.println("所有小类："+smalls);
        //3.封装到map集合
        Map<String,List<EbProductCategory>> categoryMap= new HashMap<>();
        categoryMap.put("bigs",bigs);
        categoryMap.put("smalls",smalls);
        return categoryMap;
    }

    public Map<String,List<EbProductCategory>>findBigCategory()
    {
        List<EbProductCategory> biglist=ebProductCategoryMapper.selectBigCategory();
        Map<String,List<EbProductCategory>> categoryMap=new HashMap<>();
        categoryMap.put("biglist",biglist);
        return  categoryMap;
    }

    @Override
    public int updateProductCategory(EbProductCategory ebProductCategory) {
        return ebProductCategoryMapper.updateByPrimaryKeySelective(ebProductCategory);
    }


    public  int save(EbProductCategory ebProductCategory){
//        int bigId= ebProductCategory.getEpcId();
////        System.out.println("ebProductCategory==>"+ebProductCategory);
//        EbProductCategory categoryObj=ebProductCategoryMapper.selectByPrimaryKey(bigId);
//        Integer epcParentId=categoryObj.getEpcParentId();
//        ebProductCategory.setEpcParentId(epcParentId);
////        System.out.println("ebProductCategory==>"+ebProductCategory);
//        return  ebProductCategoryMapper.insertSelective(ebProductCategory);
        if (ebProductCategory.getEpcParentId()!=0)
            return  ebProductCategoryMapper.insertSelective(ebProductCategory);
        ebProductCategoryMapper.insertSelective(ebProductCategory);
        ebProductCategory.setEpcParentId(ebProductCategory.getEpcId());
        return ebProductCategoryMapper.updateByPrimaryKey(ebProductCategory);
    }

    @Override
    public EbProductCategory selectOne(Integer epcId) {
        System.out.println("EbProductCategory==>"+epcId);
        return ebProductCategoryMapper.selectOne(epcId);
    }

//    @Override
//    public Map<String, List<EbProductCategory>> findAllCategory() {
//        List<EbProductCategory> listall=ebProductCategoryMapper.selectall();
//        Map<String,List<EbProductCategory>> categoryMap=new HashMap<>();
//        categoryMap.put("listall",listall);
////        System.out.println("listall==>"+listall);
//        return  categoryMap;
//    }

    @Override
    public  int deleteProductCategory(Integer epcId){
        System.out.println("EbProductCategory==>"+epcId);
        return ebProductCategoryMapper.deleteByPrimaryKey(epcId);
    }

}

